﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Net.Mail;
using FreeProperty.Data;

namespace FreeProperty
{
    public static class FreePropertyUtil
    {

        public static List<NameValue> GetNameValueFromCache(string nameValueType)
        {
            List<NameValue> lstNameValue = new List<NameValue>();
            if (HttpContext.Current.Cache[nameValueType] == null)
            {
                lstNameValue = GetNameValueItems(nameValueType);
                HttpContext.Current.Cache[nameValueType] = lstNameValue;
            }
            else
            {
                lstNameValue = (List<NameValue>)HttpContext.Current.Cache[nameValueType];
            }
            return lstNameValue;
        }
        public static List<NameValue> GetNameValueItems(string nameValueType)
        {
            List<NameValue> lstNameValue = new List<NameValue>();
            switch (nameValueType)
            {
                case FreePropertyConstants.CACHE_PROPERTY_TYPE:
                    lstNameValue = GetPropertTypes();
                    break;
                case FreePropertyConstants.CACHE_ALL_STATES:
                    lstNameValue = GetStates();
                    break;
                case FreePropertyConstants.CACHE_USAGE_TYPES:
                    lstNameValue = GetUsageTypes();
                    break;
                
            }
            return lstNameValue;
        }
        private static  List<NameValue> GetPropertTypes()
        {
            List<NameValue> lstPropertyTypes = new List<NameValue>();
            lstPropertyTypes.Add(new NameValue { Name = "Residential",Value="1"});
            lstPropertyTypes.Add(new NameValue { Name = "Commercial", Value = "2" });
            return lstPropertyTypes;
        }
        private static List<NameValue> GetUsageTypes()
        {
            List<NameValue> lstUsageTypes = new List<NameValue>();
            lstUsageTypes.Add(new NameValue { Name = "Buy", Value = Convert.ToInt32(FreePropertyUsageType.Buy).ToString() });
            lstUsageTypes.Add(new NameValue { Name = "Sale", Value = Convert.ToInt32(FreePropertyUsageType.Sale).ToString() });
            lstUsageTypes.Add(new NameValue { Name = "For Rent", Value = Convert.ToInt32(FreePropertyUsageType.ForRent).ToString() });
            lstUsageTypes.Add(new NameValue { Name = "To Rent", Value = Convert.ToInt32(FreePropertyUsageType.ToRent).ToString() });

            return lstUsageTypes;
        }
        private static List<NameValue> GetStates()
        {
            List<NameValue> lstStates = new List<NameValue>();
            State oState = new State();
            List<State> lstAllStates = oState.LoadAll();
            foreach (State s in lstAllStates)
            {
                lstStates.Add(new NameValue { Name = s.StateName, Value = s.StateID.ToString() });
            }
            return lstStates;
        }
        public static void BindNameValue(DropDownList ddl, string nameValueType)
        {
            ddl.DataSource = GetNameValueFromCache(nameValueType);
            ddl.DataTextField = "Name";
            ddl.DataValueField = "Value";
            ddl.DataBind();
        }
        public static void BindDropdown(DropDownList ddl, List<NameValue> lstNameValue , bool bSelectRequired)
        {
            if (bSelectRequired)
            {
                lstNameValue.Add(new NameValue { Name="---Select---",Value="-1"});
            }
            ddl.DataSource = lstNameValue;
            ddl.DataTextField = "Name";
            ddl.DataValueField = "Value";
            ddl.DataBind();
        }

        public static void SendMail(string emailId,string htmlAttachment)
        {
            
                MailMessage message = new MailMessage();
                message.From = new MailAddress(FreePropertySettings.AdminEmail);
                message.To.Add(new MailAddress(emailId));
                message.CC.Add(new MailAddress(FreePropertySettings.AdminEmail));
                message.Subject = "Provider search results from Block Vision";

                message.IsBodyHtml = true;

                message.Body = "Dear User,<br><br>Please find the search results as appended.<br><br>Regards,<br>Block Vision Admin <br><br>" + htmlAttachment;
                message.Priority = MailPriority.Normal;
                SmtpClient SmtpClient = new SmtpClient(FreePropertySettings.SMTPServer);
                if (FreePropertySettings.SMTPUserName != string.Empty)
                {
                    SmtpClient.Credentials = new System.Net.NetworkCredential(FreePropertySettings.SMTPUserName, FreePropertySettings.SMTPUserPassword);
                }
                else
                {
                    SmtpClient.UseDefaultCredentials = true;
                }
                if (FreePropertySettings.EnableSsl)
                {
                    SmtpClient.EnableSsl = FreePropertySettings.EnableSsl;
                }
                if (FreePropertySettings.SMTPPort > 0)
                {
                    SmtpClient.Port = FreePropertySettings.SMTPPort;
                }
                SmtpClient.Send(message);
            }

        
        
    }

}